<?php
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class categories extends general
{
    public $arrqx;
    public function __construct()
    {
        parent::__construct();
        $this->tpl_title = T("Dashboard");
        $this->navigation_current = 'categories';
        $this->categories = spClass('categoriesModel')->spLinker()->findAll(null,'sort_order asc,categories_id asc');
    }
    function index()
    {
        $tree = spClass('Tree');
        $fields = array('categories_id', 'parent_id');
        $tree->treelist($this->categories, $fields);
        $arr = $tree->leaf(0);
        $this->arr = $arr;
        $this->display("admin/categories_index.html");
    }
    function add()
    {
        $this->categories_status = array(0 => '头部', 1 => '左部', 2 => '不显');
        $categories_id = $this->spArgs("categories_id", 0);
        $tree = spClass('Tree');
        $fields = array('categories_id', 'parent_id');
        $tree->treelist($this->categories, $fields);
        $arr = $tree->leaf(0);
        $this->arr = $arr;
        $this->parent_id = $categories_id;
        $this->addtitle = "添加新分类";
        $this->display("admin/categories_add.html");
    }
    function edit()
    { 
        $categories_id = $this->spArgs("categories_id", 0);
        $Obj = spClass('categoriesModel');
        $conditions = array("categories_id" => $categories_id);
        $categories = $Obj->spLinker()->find($conditions);
        $this->categorie = $categories;
        $allimg=$categories['allimg'];
        if($allimg<>""){
        $allimg = unserialize($allimg);
        $i = count($allimg) - 1;
        foreach ($allimg as $key => $value) {
            if ($key == $i) {
                $img .= $value;
            } else {
                $img .= $value . '|';
            }
        }
        $this->img = $img;
        }
        $this->allimg = $allimg;
        $parent_id = $categories["parent_id"];
        $tree = spClass('Tree');
        $fields = array('categories_id', 'parent_id');
        $tree->treelist($this->categories, $fields);
        $arr = $tree->leaf(0);
        $this->arr = $arr;
        $this->parent_id = $parent_id;
        $this->addtitle = "修改分类";
        $this->display("admin/categories_add.html");
    }
    function save()
    {
        $categories_id = $this->spArgs("categories_id", 0);
        $allimg = $this->spArgs("allimg");
        $img=$this->spArgs('uploadList');
        if($allimg<>""){
        $allimg = explode('|', $allimg);
        $allimg = array_unique($allimg);
        $allimg = serialize($allimg);
        }
        $keywords=$this->spArgs("categories_keywords");
        $parent_id=$this->spArgs("parent_id");
        $sort_order=$this->spArgs("sort_order");
        $alias=$this->spArgs('categories_alias');
        $categories_name=$this->spArgs("categories_name");
        $categories_title=$this->spArgs("categories_title");
        $categories_keywords=$keywords;
        $categories_description=$this->spArgs("categories_description");
        $categories_meta_description=$this->spArgs("categories_meta_description");
        $dataform = array(
        'parent_id' => $parent_id,
        'alias'=>$alias,
        'image'=>$img,
        'allimg'=>$allimg,
        'sort_order' => $sort_order,
            'detail' => array(
            'categories_id' => $categories_id,
            'categories_name' => $categories_name,
            'categories_title' => $categories_title,
            'categories_keywords' => $categories_keywords,
            'categories_description' => $categories_description,
            'categories_meta_description' => $categories_meta_description,
            ),
        );
        $savepath.=$categories_name;
        $Obj = spClass('categoriesModel');
        if ($categories_id == 0)
        {
            $Obj->spLinker()->create($dataform);
            $msg = "分类".$dataform[categories_name]."添加成功";
        } else
        {
            $condition = array('categories_id' => $categories_id);
            $Obj->spLinker()->update($condition, $dataform);
            $msg = "分类".$dataform[categories_name]."修改成功";

        }
        Savelog($msg);
        $result = array(
        'status' => 'y', // 标志
        'info' => $msg, // 提示信息
            );
        echo json_encode($result); // 返回（显示）JSON结果
    }
    function checkalias()
    {
        $cid=$this->spArgs('cid');
        $alias=$this->spArgs('param');
        if($alias<>''){
            $aliasobj=spClass('categoriesModel')->aliascheck($alias,$cid);
            if($aliasobj){
                echo "y";
            }else{
                echo "存在重复别名,请更换！";
            }
        }else{
            echo "请输入网址别名！";
        }
    }
    function del()
    {
        $Obj = spClass("categoriesModel");

        $id2 = $this->spArgs("categories_id", 0);
        if ($id2 == 0)
        {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);

            foreach ($idsarray as $id)
            {
                $conditions = array('categories_id' => $id);
                    $arr = $Obj->find($conditions, '', 'categories_id,allimg');
                    $allimg=$arr['allimg'];
                    if($allimg<>""){
                        $allimg = unserialize($allimg);
                        foreach ($allimg as $img) {
    
                            if (file_exists("upload/" . $img)) {
                                @unlink("upload/" . $img);
                            }
                        }
                    }
                $Obj->spLinker()->delete($conditions); // 删除记录
            }
        } else
        {
            $conditions = array('categories_id' => $id2);
                    $arr = $Obj->find($conditions, '', 'categories_id,allimg');
                    if($allimg<>""){
                        $allimg = unserialize($allimg);
                        foreach ($allimg as $img) {
    
                            if (file_exists("upload/" . $img)) {
                                @unlink("upload/" . $img);
                            }
                        }
                    }
            $Obj->spLinker()->delete($conditions); // 删除记录
        }
        $msg="删除分类 ".$id2;
        Savelog($msg);
        $this->success(T('Successfully  deleted!'), spUrl("categories", "index"));

    }
    function order()
    {
        $selids = $this->spArgs("selids");
        $orderid = $this->spArgs("orderid");
        $Obj = spClass('categoriesModel');
        $selarray = explode(',', $selids);
        foreach ($selarray as $categories_id)
        {
            $condition = array('categories_id' => $categories_id);
            $dataform = array('sort_order' => $orderid[$categories_id]);
            $Obj->update($condition, $dataform);
        }
        $this->success(T('Successfully Order!'), spUrl("categories", "index"));
    }
}